package org.awa.demo.opencv.gltf.newhandler;

import lombok.extern.slf4j.Slf4j;
import org.awa.demo.opencv.gltf.Constant;
import org.awa.demo.opencv.gltf.SoundUtil;
import org.locationtech.jts.math.Vector3D;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class NewFloorHandler implements INewRectangleHandler {
    @Override
    public List<Vector3D> getRectanglePointList(List<Vector3D> pointList) {
        //根据地面特性，Z应该是固定的。
        // 所以根据x的最大最小值可以直接拿到对应y的值。
        // 面的四个点分别是[minX,minY,minZ],[maxX,maxY,minZ],[minX,minY,maxZ],[maxX,maxY,maxZ]
        double maxX = Constant.MIN_FLOAT;
        double minX = Constant.MAX_FLOAT;
        double maxY = Constant.MIN_FLOAT;
        double minY = Constant.MAX_FLOAT;
        double z = pointList.get(0).getZ();
        for (int i = 0; i < pointList.size(); i++) {
            Vector3D point  = pointList.get(i);
            double x = point.getX();
            double y = point.getY();

            if(maxX < x){
                maxX = x;
            }
            if(minX > x){
                minX = x;
            }
            if(maxY < y){
                maxY = y;
            }
            if(minY > y){
                minY = y;
            }
        }

        List<Vector3D> resultList = new ArrayList<>();
        resultList.add(new Vector3D(minX,maxY,z));
        resultList.add(new Vector3D(maxX,maxY,z));
        resultList.add(new Vector3D(maxX,minY,z));
        resultList.add(new Vector3D(minX,minY,z));

        return resultList;
    }

    @Override
    public List<Vector3D> getGridObject(List<Vector3D> pointList, int widthSize, int heightSize) {
        Vector3D firstPoint = pointList.get(0);
        double z = firstPoint.getZ();

        double[] xArr = SoundUtil.getPointListByLine(pointList.get(0).getX(),pointList.get(1).getX(),widthSize);
        double[] yArr = SoundUtil.getPointListByLine(pointList.get(1).getY(),pointList.get(2).getY(),heightSize);

        List<Vector3D> resultList = new ArrayList<>();

        for (int i = 0; i < yArr.length; i++) {
            double y = yArr[i];
            for (int j = 0; j < xArr.length; j++) {
                double x = xArr[j];
                resultList.add(new Vector3D(x,y,z));
            }
        }
        return resultList;
    }
}
